package org.mparts.client.ui;

import org.mparts.client.content.Images;
import org.mparts.client.content.Strings;
import org.mparts.client.utils.Utils;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.logical.shared.AttachEvent.Handler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.maps.gwt.client.GoogleMap;
import com.google.maps.gwt.client.InfoWindow;
import com.google.maps.gwt.client.InfoWindowOptions;
import com.google.maps.gwt.client.LatLng;
import com.google.maps.gwt.client.MapOptions;
import com.google.maps.gwt.client.MapTypeId;
import com.google.maps.gwt.client.Marker;
import com.google.maps.gwt.client.MarkerOptions;
import com.google.maps.gwt.client.MouseEvent;
import com.google.maps.gwt.client.Marker.ClickHandler;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.Composite;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer.HorizontalLayoutData;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;

public class AddressView extends Composite implements IAddressView {
	
	public interface Binder extends UiBinder<Widget, AddressView> {
	}

	private IAddressPresenter presenter;
	private VerticalLayoutContainer vGoogleCnt;
	private MapOptions myOptions;
	private LatLng myLatLng;
	private MarkerOptions markerOptions;
	
	private Strings strings;
	private Images images;
	
	@UiField
	VerticalLayoutContainer vCont;
	
//	@UiField
//	HTML map;
	
	@UiField
	Frame mapFrame;
	
	private boolean loadFlag;
	

	@Inject
	public AddressView(Images images, Strings strings, Binder uiBinder) {
		loadFlag = false;
		vCont = (VerticalLayoutContainer) uiBinder.createAndBindUi(this);
		if (Utils.getCurrentStoreId() == 2L) {
			mapFrame.setUrl("maptor.html");
		}
		initWidget(vCont);
		
		vCont.addResizeHandler(new ResizeHandler() {
			
			@Override
			public void onResize(ResizeEvent event) {
				Scheduler.get().scheduleDeferred(new ScheduledCommand() {
					
					@Override
					public void execute() {
						Log.debug("reload map frame...");
						mapFrame.setUrl(mapFrame.getUrl());
					}
				});
			}
		});
		
//		map.addAttachHandler(new Handler() {
//			
//			@Override
//			public void onAttachOrDetach(AttachEvent event) {
//				if (event.isAttached() && !loadFlag) {
//					loadFlag = true;
//					Scheduler.get().scheduleDeferred(new ScheduledCommand() {
//						
//						@Override
//						public void execute() {
//							Log.debug("load 2GIS map...");
//							loadMap();
//						}
//					});
//				}
//			}
//		});
		
//		map.addAttachHandler(new Handler() {
//			
//			@Override
//			public void onAttachOrDetach(AttachEvent event) {
//				if (event.isAttached() && !loadFlag) {
//					loadFlag = true;
//					Log.debug("load 2GIS script...");
//					ScriptInjector
//							.fromUrl(
//									"http://firmsonmap.api.2gis.ru/js/DGWidgetLoader.js")
//							.setCallback(new Callback<Void, Exception>() {
//								@Override
//								public void onFailure(Exception reason) {
//									Log.error("Script 2GIS load failed.");
//								}
//
//								@Override
//								public void onSuccess(Void result) {
//									Log.debug("2GIS script loaded successfull.");
//									ScriptInjector.fromString("alert(\"2gis\");new DGWidgetLoader({\"borderColor\":\"#a3a3a3\",\"width\":\"900\",\"height\":\"600\",\"wid\":\"3ab3683b5559666fa63def521182c0f1\",\"pos\":{\"lon\":\"82.887058033114\",\"lat\":\"54.951384137867\",\"zoom\":\"18\"},\"opt\":{\"ref\":\"inside\",\"card\":[\"name\",\"contacts\",\"schedule\",\"payings\",\"flamp\"],\"city\":\"novosibirsk\"},\"org\":[{\"id\":\"141265770581842\"}]});").setRemoveTag(false).inject();
////									map.setHTML("<script charset=\"utf-8\" type=\"text/javascript\"></script>");
//								}
//							}).inject();
//				}
//
//			}
//		});
		//<script charset=\"utf-8\" type=\"text/javascript\">new DGWidgetLoader({\"borderColor\":\"#a3a3a3\",\"width\":\"900\",\"height\":\"600\",\"wid\":\"3ab3683b5559666fa63def521182c0f1\",\"pos\":{\"lon\":\"82.887058033114\",\"lat\":\"54.951384137867\",\"zoom\":\"18\"},\"opt\":{\"ref\":\"inside\",\"card\":[\"name\",\"contacts\",\"schedule\",\"payings\",\"flamp\"],\"city\":\"novosibirsk\"},\"org\":[{\"id\":\"141265770581842\"}]});</script>
	}
		
		
		private void dumb() {
		
		VerticalLayoutContainer vCnt = new VerticalLayoutContainer();
		

		ContentPanel panel = new ContentPanel();
		panel.setHeadingText(strings.menu3());
		
		vCnt.add(panel, new VerticalLayoutData(1, 1));
		
		
		//TODO ������� �������������� ��������� � � ���� ��������� ����� ���� � �������, ������ �����	
		HorizontalLayoutContainer hCont = new HorizontalLayoutContainer();
		
		VerticalLayoutContainer vCont = new VerticalLayoutContainer();
		vCont.addStyleName("subtitle");
		
		
		SimpleContainer p = new SimpleContainer();
		p.setHeight(30);
		
//		HTML address = new HTML(strings.addressText());
//		address.addStyleName("subtitle");
		
		
		HTML addTitle = new HTML(strings.addressTitle() + ":");
		addTitle.addStyleName("addresstitle");
		vCont.add(addTitle, new VerticalLayoutData(-1, -1, new Margins(10, 5, 0, 5)));
		vCont.add(new HTML(strings.addressText()), new VerticalLayoutData(-1, -1, new Margins(5, 5, 0, 15)));
		HTML phoneTitle = new HTML(strings.phoneTitle());
		phoneTitle.addStyleName("addresstitle");
		vCont.add(phoneTitle, new VerticalLayoutData(-1, -1, new Margins(10, 5, 0, 5)));
		vCont.add(new HTML(strings.phone1()), new VerticalLayoutData(-1, -1, new Margins(5, 5, 0, 15)));
		vCont.add(new HTML(strings.phone2()), new VerticalLayoutData(-1, -1, new Margins(5, 5, 0, 15)));
		vCont.add(new HTML(strings.phone3()), new VerticalLayoutData(-1, -1, new Margins(5, 5, 0, 15)));
		HTML emailTitle = new HTML(strings.emailTitle());
		emailTitle.addStyleName("addresstitle");
		vCont.add(emailTitle, new VerticalLayoutData(-1, -1, new Margins(10, 5, 0, 5)));
		Anchor emailLink = new Anchor(strings.email(), "mailto:" + strings.email());
		vCont.add(emailLink, new VerticalLayoutData(-1, -1, new Margins(5, 5, 0, 15)));
		
		
		hCont.add(vCont, new HorizontalLayoutData(300, 1, new Margins(5)));
		
		
		
		
		
		vGoogleCnt = new VerticalLayoutContainer();
		vGoogleCnt.setBorders(false);
		
		//vGoogleCnt.add(new HTML( "<iframe width='925' height='550' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='http://maps.google.com/maps?q=54.951757,82.887264&amp;num=1&amp;t=h&amp;ie=UTF8&amp;z=14&amp;ll=54.951757,82.887264&amp;output=embed'></iframe>" ), vGData);
		
		
		myLatLng = LatLng.create(54.951757, 82.887264);
	    myOptions = MapOptions.create();
	    myOptions.setZoom(17.0);
	    myOptions.setCenter(myLatLng);
	    myOptions.setMapTypeId(MapTypeId.ROADMAP);
	    
	    

//		folder.add( vGoogleCnt, Utils.getStrings().addressMap() );
		hCont.add( vGoogleCnt, new HorizontalLayoutData(1, 1, new Margins(5)) );
		panel.add(hCont);
		
		initWidget(vCnt);
		
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
	          @Override
	          public void execute() {
	        	  showMap();
	          }
	        });
		
	
	}

	@Override
	public void setPresenter(IAddressPresenter presenter) {
		this.presenter = presenter;		
	}


		 
	public void showMap() {
		vGoogleCnt.clear();
		final GoogleMap map = GoogleMap.create(vGoogleCnt.getElement(), myOptions); 
	    markerOptions = MarkerOptions.create();
	    markerOptions.setPosition(myLatLng);
	    markerOptions.setMap(map);
	    markerOptions.setTitle(Utils.getStrings().mainText()); 
	    InfoWindowOptions infowindowOpts = InfoWindowOptions.create();
	    infowindowOpts.setContent(Utils.getStrings().mainText());
	    final InfoWindow infowindow = InfoWindow.create(infowindowOpts);
	 	final Marker myMarker = Marker.create(markerOptions);
	    myMarker.addClickListener(new ClickHandler() {
	        public void handle(MouseEvent event) {
	          infowindow.open(map, myMarker);
	        }
	      }); 	 	
	    
	}

//	public Widget asWidget() {
//		return panel;
//	}
	
	public static native void loadMap() /*-{
		$wnd.alert(" " + $wnd.document.all.map2gis.innerHTML);
		$wnd.mapLoad();
	}-*/;

}
